package com.hj.ct.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class DateUtil {
	// private static final String STR_YESTERDAY = "昨天", STR_QIANTIAN = "前天";

	private static String getTimeByLong(long tLong, String format) {
		String strDate = "";
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(tLong);
		cal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		strDate = sdf.format(cal.getTime());
		return strDate;
	}

	/**
	 * 当前时间与消息时间相差几天
	 * 
	 * @param msgTime
	 * @return
	 */
	private static int getDaysBettweenCurrTimeWhitMsgTime(long msgTime) {
		Calendar calendar = Calendar.getInstance();
		int currentYear = calendar.get(Calendar.YEAR);
		int currentMonth = calendar.get(Calendar.MONTH);
		int currentDate = calendar.get(Calendar.DATE);

		calendar.setTimeInMillis(msgTime);
		int messageYear = calendar.get(Calendar.YEAR);
		int messageMonth = calendar.get(Calendar.MONTH);
		int messageDate = calendar.get(Calendar.DATE);

		calendar.setTimeInMillis(msgTime);
		calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));

		int days = 0;
		// 同一年同一个月内的日期
		if (messageYear == currentYear && messageMonth == currentMonth) {
			days = currentDate - messageDate;
		} else if (messageYear == currentYear) {
			// 同一年，不同月 这只是一个估计值，能区别出是不同月即可
			days = (currentMonth - messageMonth) * 30;
		} else {
			// 这只是一个估计值，能区别出是不同年即可
			days = (currentYear - messageYear) * 12 * 30;
		}
		return days;
	}

	/**
	 * 以据如下规则计算给定时间 １，1小时内显示具体时分，如‘20:30’ ２,大于1小时小于24小时，显示‘*小时前’ ３,大于24小时，显示‘*天前’ ４,大于7天后，显示具体日期，‘11-28’
	 * 
	 * @param time
	 * @return
	 */
//	public static String formatResponseTime(long time) {
//		String sTime = null;
//		// time = time * 1000;
//
//		int days = getDaysBettweenCurrTimeWhitMsgTime(time);
//		if (days == 0) {
//			// 表明在当天
//			sTime = getTimeByLong(time, "HH:mm");
//
//			// 现在是24小时制，不需要下面代码
//			// String hourStr = sTime.substring(0, 2);
//			// int time = StringUtil.parseInt(hourStr, 0);
//			// if (time >= 0 && time < 6) {
//			// sTime = "凌晨" + sTime;
//			// } else if (time >= 6 && time < 12) {
//			// sTime = "上午" + sTime;
//			// } else if (time >= 12 && time < 19) {
//			// sTime = "下午" + sTime;
//			// } else if (time >= 19 && time < 24) {
//			// sTime = "晚上" + sTime;
//			// }
//			// } else if (days == 1) {
//			// sTime = STR_YESTERDAY;
//			// } else if (days == 2) {
//			// sTime = STR_QIANTIAN;
//		} else {
//			// 三天以上
//			sTime = getTimeByLong(time, "yy-MM-dd");
//		}
//		return sTime;
//	}

	public static String formatResponseTime(long time, int dur) {
		String sTime = null;
		if (dur <= 24 * 60) {
			sTime = getTimeByLong(time, "HH:mm");
		} else {
			sTime = getTimeByLong(time, "yy-MM-dd");
		}
		return sTime;
	}

}
